package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pojo.Rest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RestMapper extends BaseMapper<Rest> {
    @Select("<script>" +
            "SELECT r.reason,r.days,r.opinion,r.status,r.create_time,r.update_time,r.user_id, u.nickname " +
            "FROM rest r " +
            "LEFT JOIN user u ON r.user_id = u.id " +
            "WHERE 1=1 " +  // 保证后续条件可以直接用 AND
            "<if test='isAdmin != \"0\"'> " +
            "   AND r.user_id = #{userId} " +
            "</if> " +
            "<if test='nickname != null and nickname != \"\"'> " +
            "   AND u.nickname = #{nickname} " +
            "</if> " +
            "<if test='search != null and search != \"\"'> " +
            "   AND (u.nickname LIKE CONCAT('%', #{search}, '%')) " +
            "</if> " +
            "</script>")
    List<Rest> selectWithLeftJoin(@Param("isAdmin") String isAdmin,
                                  @Param("nickname") String nickname,
                                  @Param("search") String search,
                                  @Param("userId") Integer userId);


}
